package service

import (
	"server-admin-api/dto"
	"server-admin-api/message"
	"server-admin-api/models"
	"server-admin-api/views"
)

type GroupService struct {
	dto *dto.GroupDto
}

func NewGroupService() *GroupService {
	return &GroupService{
		dto: dto.NewGroupDto(),
	}
}

func (g *GroupService) GroupPage(page, size int, title string) (message.DataPage, error) {
	count, err := g.dto.Count()
	if err != nil {
		return message.ErrorDataPage(), err
	}
	finds, err := g.dto.List(page, size, title)
	if err != nil {
		return message.ErrorDataPage(), err
	}
	return message.NewDataPage(finds, count, page, size), nil
}


// CreateGroup 创键分组
func (g *GroupService) CreateGroup(id uint, param views.GroupCreate) error {
	var data models.Group
	if id == 0 {
		data = models.Group{}.NewBuild(param)
		return g.dto.Insert(data)
	} else {
		return g.dto.Update(id, param)
	}
}

func (g *GroupService) DeleteById(id uint) bool {
	if err := g.dto.DeleteOne(id); err != nil {
		return false
	}
	return true
}



